﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BuildHelper.Work.JAVA;
using DevExpress.XtraEditors;
using ProgNetComponentsDx.Options;

namespace BuildHelper.Work.Short
{
    public partial class ShortJavaControl : XtraUserControl
    {
        public ShortJavaControl()
        {
            InitializeComponent();
        }

        private BudowanieJavy _JavaForm;
        public BudowanieJavy Form
        {
            get { return _JavaForm; }
            set
            {
                _JavaForm = value;
                if (value != null)
                {
                    lblBrancz.Text = value.Brancz;
                    if (!value.Pojedyncza)
                        labelControl1.Text += " (zmienny)";
                    value.BranczChanged += new EventHandler(value_BranczChanged);
                    gcMain.DataSource = value.javaDataSet;
                }
                else
                {
                    gcMain.DataSource = null;
                }
            }
        }
        void value_BranczChanged(object sender, EventArgs e)
        {
            lblBrancz.Text = Form.Brancz;
        }

        private void btnGuiWczytajTemplate_Click(object sender, EventArgs e)
        {
            if (Form != null)
                Form.WczytajTemplate();
        }

        private void btnBuduj_Click(object sender, EventArgs e)
        {
            if (Form != null)
                Form.Buduj();
        }

        private void btnDeployEar_Click(object sender, EventArgs e)
        {
            if (Form != null)
                Form.DeployEar();
        }

        private void btnRunSerwer_Click(object sender, EventArgs e)
        {
            if (Form != null)
                Form.RunServer();
        }

        private void btnAnuluj_Click(object sender, EventArgs e)
        {
            if (Form != null)
                Form.Anuluj();
        }

        private void btnPokazPelnyLog_Click(object sender, EventArgs e)
        {
            if (Form != null)
                Form.PokazPelnyLog();
        }

        public void PoczatekBudowania()
        {
            if (progress.InvokeRequired)
            {
                progress.Invoke(new Action(PoczatekBudowania));
                return;
            }
            progress.Properties.Stopped = false;
        }

        public void KoniecBudowania()
        {
            if (progress.InvokeRequired)
            {
                progress.Invoke(new Action(KoniecBudowania));
                return;
            }
            progress.Properties.Stopped = true;
        }

        private void modeProvider1_CustomGetModes(object sender, ProgNetComponentsDx.Mode.CustomModeSettingsEventArgs e)
        {
            if (sender == colPakuj && e.Row != null && e.Row["STM"].AsString() == "GCM")
            {
                e.IsReadOnly = true;
            }
        }
        bool lockChange = false;
        private void gvMain_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            DataRow r = gvMain.GetFocusedDataRow();
            if (r["STM"].AsString() != "GCM")
            {
                if (ceZmieniajRazem.Checked && !lockChange)
                {
                    lockChange = true;
                    if (e.Column == colBuduj)
                    {
                        gvMain.SetRowCellValue(e.RowHandle, colPakuj, e.Value.AsBool());
                    }
                    else if (e.Column == colPakuj)
                    {
                        gvMain.SetRowCellValue(e.RowHandle, colBuduj, e.Value.AsBool());
                    }
                    lockChange = false;
                }
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            ceZmieniajRazem.Checked = OptionCache.GetOptionBoolValue(Init.InitFormOptionNames.Work_Group.Java.ZmieniajRazem);
        }
    }
}
